package edu.njfu.mystudentapp

import android.graphics.Bitmap
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager

import edu.njfu.mystudentapp.adapter.StudentAdapterByRecyclerView
import edu.njfu.mystudentapp.model.Student
import edu.njfu.mystudentapp.service.StudentService
import edu.njfu.mystudentapp.utils.ServerCreator
import kotlinx.android.synthetic.main.activity_students_info.*
import retrofit2.Call
import retrofit2.Response

class SearchActivity : AppCompatActivity() {
    var bitmap: Bitmap? =null
    lateinit var students:List<Student>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_students_info)
        var resultByName=intent.getStringExtra("stduentName")

        if (resultByName != null) {
            Log.d("asdas",resultByName)
        }else{
            Log.d("asdas","dsfsd")

        }
        val serviceCreator= ServerCreator.create<StudentService>()
        serviceCreator.getStudentData().enqueue(object :retrofit2.Callback<List<Student>>{
            override fun onFailure(call: Call<List<Student>>, t: Throwable) {
                Toast.makeText(this@SearchActivity,"无法连接网络",Toast.LENGTH_SHORT).show()

            }

            override fun onResponse(call: Call<List<Student>>, response: Response<List<Student>>) {
                Log.d("null","type==1")

                students = response.body()!!
                if(resultByName==null){
//                    var layoutManager=LinearLayoutManager(this@SearchActivity)
//                    recyclerView.layoutManager=layoutManager
//                    var adapter= students?.let { StudentAdapterByRecyclerView(it) }
//                    recyclerView.adapter=adapter
                    Log.d("null","resultName + +    :isg is nul;l")
                    Toast.makeText(this@SearchActivity,"查询为空", Toast.LENGTH_LONG).show()
                }else{
                    Log.d("null","type==1")
                    var searchList=ArrayList<Student>()
                    students.forEach{
                        if(it.stuName==resultByName){
                            searchList?.add(it)
                            Log.d("null",it.toString())


                        }
//                        Log.d("null",it.toString())

                    }
                    if (searchList != null) {
                        if(searchList.size==0){
                            Log.d("null","resuode     :isg is nul;l")
                            Toast.makeText(this@SearchActivity,"查询无结果", Toast.LENGTH_LONG).show()
                        }else{
                            Log.d("null","resuode   aaaaa  :isg is nul;l")

                            val layoutManager= LinearLayoutManager(this@SearchActivity)
                            recyclerView.layoutManager=layoutManager
                            val adapter= searchList?.let { StudentAdapterByRecyclerView(it) }
                            recyclerView.adapter=adapter
                        }
                    }else{
                        Log.d("null","lsearchL:isg is nul;l")

                    }
                }


            }
        })
        searchBtn.setOnClickListener{
            var name=searchName.text.toString();
            val serviceCreator= ServerCreator.create<StudentService>()
            serviceCreator.getStudentData().enqueue(object :retrofit2.Callback<List<Student>>{
                override fun onFailure(call: Call<List<Student>>, t: Throwable) {
                    TODO("Not yet implemented")
                }
                override fun onResponse(call: Call<List<Student>>, response: Response<List<Student>>) {
                    Log.d("null","type==1")

                    students = response.body()!!
                    if(name==null){
                        Log.d("null","resultName + +    :isg is nul;l")
                        Toast.makeText(this@SearchActivity,"查询为空", Toast.LENGTH_LONG).show()
                    }else{
                        Log.d("null","type==1")
                        var searchList=ArrayList<Student>()
                        students.forEach{
                            if(it.stuName==name){
                                searchList?.add(it)
                                Log.d("null",it.toString())


                            }
//                        Log.d("null",it.toString())

                        }
                        if (searchList != null) {
                            if(searchList.size==0){
                                Log.d("null","resuode     :isg is nul;l")
                                Toast.makeText(this@SearchActivity,"查询无结果", Toast.LENGTH_LONG).show()
                            }else{
                                Log.d("null","resuode   aaaaa  :isg is nul;l")

                                val layoutManager= LinearLayoutManager(this@SearchActivity)
                                recyclerView.layoutManager=layoutManager
                                val adapter= searchList?.let { StudentAdapterByRecyclerView(it) }
                                recyclerView.adapter=adapter
                            }
                        }else{
                            Log.d("null","lsearchL:isg is nul;l")

                        }
                    }


                }
            })

        }
    }
}